﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.Windows.Controls.Primitives;
using System.ComponentModel;

namespace SL8.SL.Controls
{
    [TemplatePart(Name = AppChromeControl.PART_AppIconImage, Type = typeof(Image))]
    [TemplatePart(Name = AppChromeControl.PART_AppTitleTextBlock, Type = typeof(TextBlock))]
    [TemplatePart(Name = AppChromeControl.PART_MinimizeButton, Type = typeof(Button))]
    [TemplatePart(Name = AppChromeControl.PART_RestoreButton, Type = typeof(Button))]
    [TemplatePart(Name = AppChromeControl.PART_CloseButton, Type = typeof(Button))]
    [TemplatePart(Name = AppChromeControl.PART_DragResizeBorder, Type = typeof(Border))]
    public class AppChromeControl : ContentControl
    {
        #region 静态变量
        private const string PART_AppIconImage = "AppIconImage";
        private const string PART_AppTitleTextBlock = "AppTitleTextBlock";
        private const string PART_MinimizeButton = "MinimizeButton";
        private const string PART_RestoreButton = "RestoreButton";
        private const string PART_CloseButton = "CloseButton";
        private const string PART_DragResizeBorder = "DragResizeBorder";
        private const string PART_BackgroundTiledImage = "BackgroundTiledImage";
        #endregion

        #region 依赖属性
        public static readonly DependencyProperty AppIconImageSourceProperty = DependencyProperty.Register("AppIconImageSource", typeof(ImageSource), typeof(AppChromeControl), null);

        public static readonly DependencyProperty BackgroundSourceUriProperty = DependencyProperty.Register("BackgroundSourceUri", typeof(Uri), typeof(AppChromeControl), null);
         
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(AppChromeControl), null);
        #endregion

        #region 私有和内部成员
        internal ButtonBase MinimizeButton
        {
            get;
            private set;
        }

        internal ButtonBase RestoreButton
        {
            get;
            private set;
        }

        internal ButtonBase CloseButton
        {
            get;
            private set;
        }

        internal Image AppIconImage
        {
            get;
            private set;
        }

        internal TextBlock AppTitleTextBlock
        {
            get;
            private set;
        }

        internal TiledImage BackgroundTiledImage
        {
            get;
            private set;
        }

        internal Border DragResizeBorder
        {
            get;
            private set;
        }
        #endregion

        #region 属性
        public ImageSource AppIconImageSource
        {
            get
            {
                return (ImageSource)base.GetValue(AppChromeControl.AppIconImageSourceProperty);
            }

            set
            {
                base.SetValue(AppChromeControl.AppIconImageSourceProperty, value);
            }
        }

        public Uri BackgroundSourceUri
        {
            get
            {
                return (Uri)base.GetValue(AppChromeControl.BackgroundSourceUriProperty);
            }

            set
            {
                base.SetValue(AppChromeControl.BackgroundSourceUriProperty, value);
            }
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
        #endregion

        public AppChromeControl()
        {
            this.DefaultStyleKey = typeof(AppChromeControl);
        }

        public override void OnApplyTemplate()
        {
            this.UnsubscribeFromTemplatePartEvents();
            base.OnApplyTemplate();

            this.AppIconImage = this.GetTemplateChild(AppChromeControl.PART_AppIconImage) as Image;
            this.AppTitleTextBlock = this.GetTemplateChild(AppChromeControl.PART_AppTitleTextBlock) as TextBlock;
            this.MinimizeButton = this.GetTemplateChild(AppChromeControl.PART_MinimizeButton) as ButtonBase;
            this.RestoreButton = this.GetTemplateChild(AppChromeControl.PART_RestoreButton) as ButtonBase;
            this.CloseButton = this.GetTemplateChild(AppChromeControl.PART_CloseButton) as ButtonBase;
            this.DragResizeBorder = this.GetTemplateChild(AppChromeControl.PART_DragResizeBorder) as Border;
            this.BackgroundTiledImage = this.GetTemplateChild(AppChromeControl.PART_BackgroundTiledImage) as TiledImage;

            this.SubscribeToTemplatePartEvents();

            this.UpdateResizeBorderVisual();
        }

        private void UpdateResizeBorderVisual()
        {
            if (this.DragResizeBorder == null)
            {
                return;
            }

            if (Application.Current.IsRunningOutOfBrowser)
            {
                if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
                {
                    this.DragResizeBorder.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    this.DragResizeBorder.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }

        /// <summary>
        /// 注销模版事件
        /// </summary>
        private void UnsubscribeFromTemplatePartEvents()
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }


            if (this.MinimizeButton != null)
            {
                this.MinimizeButton.Click -= new RoutedEventHandler(MinimizeButton_Click);
            }

            if (this.CloseButton != null)
            {
                this.CloseButton.Click -= new RoutedEventHandler(CloseButton_Click);
            }

            if (this.RestoreButton != null)
            {
                this.RestoreButton.Click -= new RoutedEventHandler(RestoreCheckBox_Click);
            }

            if (this.DragResizeBorder != null)
            {
                this.DragResizeBorder.MouseLeftButtonDown -= new MouseButtonEventHandler(DragResizeBorder_MouseLeftButtonDown);
            }
        }

        /// <summary>
        /// 注册事件
        /// </summary>
        private void SubscribeToTemplatePartEvents()
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }

            if (this.MinimizeButton != null)
            {
                this.MinimizeButton.Click += new RoutedEventHandler(MinimizeButton_Click);
            }

            if (this.CloseButton != null)
            {
                this.CloseButton.Click += new RoutedEventHandler(CloseButton_Click);
            }

            if (this.RestoreButton != null)
            {
                this.RestoreButton.Click += new RoutedEventHandler(RestoreCheckBox_Click);
            }

            if (this.DragResizeBorder != null)
            {
                this.DragResizeBorder.MouseLeftButtonDown += new MouseButtonEventHandler(DragResizeBorder_MouseLeftButtonDown);

            }
        }

        void DragResizeBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
            {
                Application.Current.MainWindow.DragResize(WindowResizeEdge.BottomRight);
            }
        }

        void RestoreCheckBox_Click(object sender, RoutedEventArgs e)
        {
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
            {
                if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
                {
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                    this.RestoreButton.Content = "1";
                }
                else
                {
                    Application.Current.MainWindow.WindowState = WindowState.Maximized;
                    this.RestoreButton.Content = "2";
                }
            }

            this.UpdateResizeBorderVisual();
        }

        void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
            {
                Application.Current.MainWindow.Close();
            }
        }

        void MinimizeButton_Click(object sender, RoutedEventArgs e)
        {
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
            {
                Application.Current.MainWindow.WindowState = WindowState.Minimized;
            }
        }
    }
}
